﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.Mvc;
using ShopB2C.Entity;
using ShopB2C.Helper;
using ShopB2C.Interfaces;
using ShopB2C.ViewModels;

namespace ShopB2C.Controllers.Manager {
    public partial class ConsultController : BaseController {
        private static IConsult _consult;
        private static IProductPreview _productPreView;

        public ConsultController() {
            _consult = DataBaseExecuter.CreateClassByName<IConsult>("Consult");
            _productPreView = DataBaseExecuter.CreateClassByName<IProductPreview>("ProductPreView");
        }

        #region Consult

        public ActionResult ConsultProduct(
            [DefaultValue("a")]string audit,
            [DefaultValue(1)]int productPage) {
            this.SiteMapPathAdd("包含咨询的商品");
            ViewData["Audit"] = audit;

            bool? auditB = null;
            if (!string.IsNullOrEmpty(audit) && audit != "a") {
                auditB = audit == "t" ? true : false;
            }

            int pageCount;
            return View(new ProductViewModel() {
                ProductList = _consult.GetConsultProduct(auditB, 10, productPage, out pageCount)
                    ?? new List<ProductEntity>(),
                ImageRoot = _imageRoot,
                PageNumber = productPage,
                Pager = this.CreatePager(Url.Action("ConsultProduct", new {
                    productPage = "{0}",
                    audit = audit
                }), pageCount, productPage)
            });
        }

        public ActionResult ConsultList(
            [DefaultValue("a")]string audit,
            [DefaultValue(1)]int productPage,
            [DefaultValue(0)]int productId,
            [DefaultValue(1)]int consultPage) {

            ViewData.Add("Audit", audit);
            ViewData.Add("ProductPage", productPage);
            ProductEntity product = _productPreView.ProductView(productId, 0);
            ConsultParameters cps = new ConsultParameters() {
                IsAudit = null,
                ProductId = productId,
                PageIndex = consultPage
            };
            List<ConsultEntity> consultList = _consult.GetConsultList(cps) ?? new List<ConsultEntity>();

            this.SiteMapPathAdd("包含咨询的商品", Url.Action("ConsultProduct", "Consult", new {
                audit = audit,
                productPage = productPage
            }));
            this.SiteMapPathAdd("商品咨询");

            return View(new ConsultViewModel() {
                Product = product,
                ConsultList = consultList,
                ImageRoot = _imageRoot,
                PageNumber = consultPage,
                Pager = this.CreatePager(Url.Action("ConsultList", new {
                    audit = audit,
                    productId = productId,
                    productPage = productPage,
                    consultPage = "{0}"
                }), cps.PageCount, consultPage)
            });
        }

        public ActionResult ConsultEdit(
            [DefaultValue("a")]string audit,
            [DefaultValue(1)]int productPage,
            [DefaultValue(0)]int productId,
            [DefaultValue(1)]int consultPage,
            [DefaultValue(0)]int consultId) {

            string url = Url.Action("ConsultList", new {
                audit = audit,
                productId = productId,
                productPage = productPage,
                consultPage = consultPage
            });
            this.SiteMapPathAdd("包含咨询的商品", Url.Action("ConsultProduct", "Consult", new {
                audit = audit,
                productPage = productPage
            }));
            this.SiteMapPathAdd("商品咨询", url);
            this.SiteMapPathAdd("商品咨询编辑");

            ViewData.Add("SubmitUrl", Url.Action("ConsultEdit", new {
                audit = audit,
                productId = productId,
                productPage = productPage,
                consultPage = consultPage
            }));
            ViewData.Add("ReturnUrl", url);

            ConsultEntity ce = consultId == 0
                ? new ConsultEntity() {
                    Content = string.Empty,
                    ReplyContent = string.Empty
                }
                : _consult.GetConsultById(consultId);
            ce.Content = Htmls.DecodeHtml(ce.Content);
            ce.ReplyContent = Htmls.DecodeHtml(ce.ReplyContent);
            return View(new ConsultViewModel() {
                Consult = ce,
                PageNumber = consultPage,
            });
        }

        [HttpPost]
        public ActionResult ConsultEdit(
            [DefaultValue("a")]string audit,
            [DefaultValue(1)]int productPage,
            [DefaultValue(0)]int productId,
            [DefaultValue(1)]int consultPage,
            ConsultViewModel cvm) {
            #region 检查数据合法性

            MessageHelper mh = new MessageHelper("consult");
            mh.AddBody(2);
            if (string.IsNullOrEmpty(cvm.Consult.Content)) {
                mh.AddMessage(3);
            }
            if (!mh.IsValid) {
                return this.MessageResult(mh.CreateMessage());
            }

            #endregion

            ConsultEntity ce = _consult.GetConsultById(cvm.Consult.ConsultId);
            ce.Content = Htmls.EncodeHtml(cvm.Consult.Content);
            ce.ReplyContent = Htmls.EncodeHtml(cvm.Consult.ReplyContent);
            ce.ReplyDate = DateTime.Now;
            ce.IsAudit = !string.IsNullOrEmpty(cvm.Consult.ReplyContent);
            _consult.ConsultUpdate(ce);

            return RedirectToAction("ConsultList", new {
                audit = audit,
                productId = productId,
                productPage = productPage,
                consultPage = consultPage
            });
        }

        public ActionResult ConsultDelete(
            [DefaultValue("a")]string audit,
            [DefaultValue(1)]int productPage,
            [DefaultValue(0)]int productId,
            string ids) {
            _consult.ConsultDelete(ids);

            return RedirectToAction("ConsultList", new {
                audit = audit,
                productId = productId,
                productPage = productPage
            });
        }

        #endregion
    }
}
